<template>
<div>

    <table>
       <tr v-for="(i,index) in list" :key="i.id">
        <td>
          <van-field :label="i.label" v-model="names[index]"/>
        </td>
        <td>
          <select v-model="type[index]" >
            <option value="input">输入框</option>
            <option value="radio">单选框</option>
            <option value="checkbox">多选</option>
            <option value="select">下拉菜单</option>
            <option value="date">时间</option>
            <option value="file">文件</option>
          </select>
        </td>
        <td>
          <van-field label="值" v-model="values[index]" />
          <van-button @click="getsource(index)">选择数据源</van-button>
        </td>
      </tr>
    </table>
    <van-button @click="add">+</van-button>
    <van-button @click="submit">提交</van-button>



  <el-dialog v-model="dialogTableVisible" title="Shipping address">
    <el-table
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
        @current-change="handleCurrentChange"
    >
      <el-table-column type="index" width="50" />

      <el-table-column property="name" label="Name" width="120" />

    </el-table>


  </el-dialog>


</div>
</template>

<script>
import axios from 'axios'
export default {
  name: "setparmas",
  data(){
    return{
      list:[{'label':'名称'}],
      names:{},
      type:{},
      values:{},
      dialogTableVisible:false,
      tableData:[],
      gridData:[{'id':1,'name':'请假类型数据源'},{'id':2,'name':'会议地址数据源'}],
      id:this.$route.query.id

    }
  },
  methods:{
    add(){
      this.list.push({'label':'名称'})
    },
    submit(){
        let tlist=[]
        for(var i=0;i<this.list.length;i++){
          let v= this.values[i]
          if (v){
            v = JSON.parse(v)
          }
          let dict={'lable':this.names[i],'type':this.type[i],'values':v}
          tlist.push(dict)
          console.log(tlist)
        }
        axios({url:'work/setparmas',method:'post',data:{'id':this.id,'mes':JSON.stringify(tlist)}}).then(res=>{
          alert('成功')
          if(res.code==200){
            alert(11)
          }
        })
    },
    //增加数据源
    getsource(index){
        localStorage.setItem('index',index)
        axios({url:'work/getsourcedata',method:'get'}).then(res=>{
          this.tableData = res.data.list
        })
        this.dialogTableVisible=true

    },
    handleCurrentChange(val){
      console.log(val)
      this.dialogTableVisible=false
      let index = localStorage.getItem('index')
      this.values[index] = JSON.stringify(val['mes'])
    }
  }
}
</script>

<style scoped>

</style>